<template>
  <div class="dialog__wrapper" v-if="visible" @click.self="close">
    <div class="dialog" :style="{ width }">
      <div class="dialog__header">
        <span class="dialog__title">{{ title }}</span>
        <button type="button" @click="close" class="dialog__headerbtn">
          X
        </button>
      </div>
      <div class="dialog__body">
        <!-- 默认插槽 -->
        <slot />
      </div>
      <div class="dialog__footer">
        <!-- 命名插槽 -->
        <slot name="footer" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Dialog",
  props: {
    title: {
      type: String,
      default: "",
    },
    visible: {
      type: Boolean,
      default: false,
    },
    width: {
      type: String,
      default: "50%",
    },
  },
  methods: {
    close() {
      this.$emit("update:visible", false);
    },
  },
  watch: {
    visible: {
      handler(newValue) {
        if (newValue) {
          document.documentElement.style.overflow = "hidden";
        } else {
          document.documentElement.style.overflow = "auto";
        }
      },
      immediate: true,
    },
  },
};
</script>

<style lang="less" scoped>
.dialog__wrapper {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  background-color: rgba(0, 0, 0, 0.3);
}
.dialog {
  position: relative;
  margin: 15vh auto 50px;
  background: #fff;
  border-radius: 2px;
  box-shadow: 0 1px 3px rgb(0 0 0 / 30%);
  box-sizing: border-box;
}
.dialog__header {
  padding: 20px 20px 10px;
  font-size: 18px;
  display: flex;
  justify-content: space-between;
}
.dialog__body {
  padding: 30px 20px;
  color: #606266;
  font-size: 14px;
  word-break: break-all;
  text-align: center;
}
.dialog__footer {
  padding: 10px 20px 20px;
  text-align: center;
  box-sizing: border-box;
}
</style>